﻿@using Microsoft.AspNetCore.Mvc.Rendering;
@using NonFactors.Mvc.Grid;

@model IGrid

<div id="@Model.Id" class="mvc-grid" data-name="@Model.Name" data-filter-mode="@Model.FilterMode" data-source-url="@Model.SourceUrl">
    <!table@(Model.Attributes)>
        <thead>
            <tr class="mvc-grid-headers @(Model.FilterMode == GridFilterMode.HeaderRow ? "mvc-grid-row-filters" : "")">
                @foreach (IGridColumn column in Model.Columns)
                {
                    String sortableClass = column.Sort.IsEnabled == true && Model.FilterMode != GridFilterMode.HeaderRow ? "sortable" : null;
                    String filterableClass = column.Filter.IsEnabled == true ? "filterable" : null;

                    <th class="@filterableClass @sortableClass @column.CssClasses"
                        data-name="@column.Name"
                        data-filter="@column.Filter.IsEnabled"
                        data-filter-name="@column.Filter.Name"
                        data-filter-multi="@column.Filter.IsMulti"
                        data-filter-first-method="@column.Filter.First?.Method"
                        data-filter-first-value="@column.Filter.First?.Value"
                        data-filter-operator="@column.Filter.Operator"
                        data-filter-second-method="@column.Filter.Second?.Method"
                        data-filter-second-value="@column.Filter.Second?.Value"
                        data-sort="@column.Sort.IsEnabled"
                        data-sort-order="@column.Sort.Order.ToString().ToLower()"
                        data-sort-first="@column.Sort.FirstOrder">
                        @if (column.Filter.IsEnabled == true && Model.FilterMode != GridFilterMode.FilterRow)
                        {
                            String filterApplied = (column.Filter.First ?? column.Filter.Second) != null ? "applied" : "";

                            if (Model.FilterMode == GridFilterMode.HeaderRow)
                            {
                                <div class="first-filter">
                                    @{
                                        String value = column.Filter.First?.Value;
                                        foreach (SelectListItem option in column.Filter.Options)
                                        {
                                            if (column.Filter.First?.Value == option.Value)
                                            {
                                                value = option.Text;

                                                break;
                                            }
                                        }
                                    }

                                    <input class="mvc-grid-method" type="hidden" value="@column.Filter.First?.Method" />
                                    <input class="mvc-grid-value" value="@value" placeholder="@column.Title" />
                                    <a href="#" class="mvc-grid-filter @filterApplied"></a>
                                </div>
                            }
                            else
                            {
                                <span class="mvc-grid-header-title @column.Sort.Order.ToString().ToLower()">@column.Title</span>
                                <a href="#" class="mvc-grid-filter @filterApplied"></a>
                            }

                            <select class="mvc-grid-options">
                                @foreach (SelectListItem option in column.Filter.Options)
                                {
                                    <option value="@option.Value">@option.Text</option>
                                }
                            </select>
                        }
                        else
                        {
                            <span class="mvc-grid-header-title @column.Sort.Order.ToString().ToLower()">@column.Title</span>
                        }
                    </th>
                }
            </tr>
            @if (Model.FilterMode == GridFilterMode.FilterRow)
            {
                <tr class="mvc-grid-row-filters">
                    @foreach (IGridColumn column in Model.Columns)
                    {
                        if (column.Filter.IsEnabled == true)
                        {
                            <th class="filterable @column.CssClasses">
                                @{ String filterApplied = (column.Filter.First ?? column.Filter.Second) != null ? "applied" : ""; }

                                <div class="first-filter">
                                    <input class="mvc-grid-method" type="hidden" value="@column.Filter.First?.Method" />

                                    @if (column.Filter.Options.Any())
                                    {
                                        <select class="mvc-grid-value">
                                            @foreach (SelectListItem option in column.Filter.Options)
                                            {
                                                if (column.Filter.First?.Value == option.Value)
                                                {
                                                    <option value="@option.Value" selected>@option.Text</option>
                                                }
                                                else
                                                {
                                                    <option value="@option.Value">@option.Text</option>
                                                }
                                            }
                                        </select>
                                    }
                                    else
                                    {
                                        <input class="mvc-grid-value" value="@column.Filter.First?.Value" />
                                    }

                                    <a href="#" class="mvc-grid-filter @filterApplied"></a>
                                </div>
                            </th>
                        }
                        else
                        {
                            <th class="@column.CssClasses"></th>
                        }
                    }
                </tr>
            }
        </thead>
        <tbody>
            @if (Model.Rows.Any())
            {
                foreach (IGridRow<Object> row in Model.Rows)
                {
                    <!tr class="@row.CssClasses"@row.Attributes>
                        @foreach (IGridColumn column in Model.Columns)
                        {
                            <td class="@column.CssClasses">@column.ValueFor(row)</td>
                        }
                    </!tr>
                }
            }
            else if (Model.EmptyText != null)
            {
                <tr class="mvc-grid-empty-row">
                    <td colspan="@Model.Columns.Count()">
                        @Model.EmptyText
                    </td>
                </tr>
            }
        </tbody>
        @if (Model.FooterPartialViewName != null)
        {
            <tfoot>
                @Html.Partial(Model.FooterPartialViewName, Model)
            </tfoot>
        }
    </!table>
    @if (Model.Pager != null)
    {
        @Html.Partial(Model.Pager.PartialViewName, Model.Pager)
    }
</div>
